package com.example.bloodbankapplication.fragment;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.bloodbankapplication.R;

public class SuggestFriendFragment extends BaseFragment{
	private EditText mEText1, mEText2, mEText3,mEText4,mEText5;
	private String mUser1, mUser2, mUser3,mUser4,mUser5;
	private Button request;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.requestfriend, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		mEText1 = (EditText) getView().findViewById(R.id.ed1);
		mEText2 = (EditText) getView().findViewById(R.id.ed2);
		mEText3 = (EditText) getView().findViewById(R.id.ed3);
		mEText4 = (EditText) getView().findViewById(R.id.ed4);
		mEText5 = (EditText) getView().findViewById(R.id.ed5);

		request = (Button) getView().findViewById(R.id.request);
		request.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new MYTask().execute();
			}
		});
	}

	class MYTask extends AsyncTask<Void,Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			mUser1 = mEText1.getText().toString();
			mUser2 = mEText2.getText().toString();
			mUser3 = mEText3.getText().toString();
			mUser4 = mEText4.getText().toString();
			mUser5 = mEText5.getText().toString();


			StringBuffer sb = new StringBuffer();
			sb.append(mUser1).append(",").append(mUser2).append(",").append(mUser3).append(",").append(mUser4).append(",").append(mUser5);
			Log.i("NCS", "EmailId's are" + sb.toString());

			Properties props = new Properties();
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.socketFactory.port", "465");
			props.put("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.port", "465");

			Session session = Session.getDefaultInstance(props,
					new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(
							"android.bloodbank@gmail.com", "bloodbank@123");
				}
			});

			try {
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress("android.bloodbank@gmail.com"));
				message.setRecipients(Message.RecipientType.TO, InternetAddress
						.parse(sb.toString()));
				message.setSubject("Give Blood Give Life");
				message.setContent("http://www.bloodbankindia.net/",
						"text/html; charset=utf-8");

				Transport.send(message);

			} catch (MessagingException e) {

				Log.i("NCS", "Exception is " + e.getMessage());
				e.printStackTrace();
			}
		


		return null;
	}

}

}
